<template>
  <div>
    <BasicModal
      @register="register"
      title="设备回收"
      width="600px"
      :destroyOnClose="true"
      :canFullscreen="false"
      :showOkBtn="false"
      :showCancelBtn="false"
    >
      <BasicForm @register="registerForm" />
      <div style="color: red; text-align: center"
        >回收后，原有设备绑定的活动将自动解绑，建议在和机构确认无误后再操作</div
      >
    </BasicModal>
  </div>
</template>

<script lang="ts" setup>
  import { BasicModal, useModalInner } from '@/components/Modal';
  import { BasicForm, useForm } from '@/components/Form';
  import { ref } from 'vue';
  import { useMessage } from '@/hooks/web/useMessage';
  import { DeviceDispense } from '@/api/device/deviceList';
  import { IDeviceDispenseData } from '@/api/device/deviceList/type';
  import { DispenseStatusEnum, DispenseTypeEnum } from '@/enums/device';
  import { useUserStore } from '@/store/modules/user';

  const type = ref('add');
  const { createMessage } = useMessage();

  const userStore = useUserStore();

  const emit = defineEmits(['reload']);

  const [registerForm, { validate, setProps, setFieldsValue }] = useForm({
    schemas: [
      {
        field: 'printerNo',
        component: 'Input',
        required: true,
        label: '回收设备编号',
        componentProps: {
          disabled: true,
        },
      },
      {
        field: 'tenantId',
        component: 'Input',
        label: '设备所在机构id',
        show: false,
      },
      {
        field: 'orgName',
        component: 'Input',
        required: true,
        label: '设备所在机构',
        componentProps: {
          disabled: true,
        },
      },
      {
        field: 'description',
        component: 'InputTextArea',
        label: '备注',
        componentProps: {
          rows: 4,
          placeholder: '请填写回收的原因（非必填）',
        },
      },
    ],
    autoFocusFirstItem: true,
    actionColOptions: {
      span: 24,
    },
    labelWidth: 120,
    baseColProps: { span: 20 },
    submitButtonOptions: {
      text: '确认',
    },
    resetButtonOptions: {
      text: '取消',
    },
    submitFunc: async () => {
      const data: IDeviceDispenseData = await validate();
      setProps({
        submitButtonOptions: {
          loading: true,
        },
      });
      try {
        await DeviceDispense({
          ...data,
          tenantBefore: data.tenantId as number,
          tenantBeforeName: data.orgName as string,
          tenantAfter: 0,
          tenantAfterName: '平台',
          dispenseStatus: DispenseStatusEnum.UNDISTRIBUTED,
          dispenseType: DispenseTypeEnum.RECOVERY,
          createBy: userStore.getSysUserInfo.userName,
        });
        setProps({
          submitButtonOptions: {
            loading: false,
          },
        });

        createMessage.success('回收成功');
        emit('reload');
        closeModal();
      } catch (error) {
        setProps({
          submitButtonOptions: {
            loading: false,
          },
        });
      }
    },
    resetFunc: async () => {
      closeModal();
    },
  });

  const [register, { closeModal }] = useModalInner(async (data) => {
    // console.log('data--->', data);
    type.value = data.type;

    data.row && setFieldsValue(data.row);
  });
</script>

<style lang="less" scoped></style>
